﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Croxweb.Leuphana.Models;
using System.Windows.Media.Imaging;
using Croxweb.Leuphana.Services.MyStudyService;

namespace Croxweb.Leuphana.ViewModels
{
    public class PersonDetailViewModel : ViewModelBase
    {
        private readonly IMyStudyService service;

        private PersonDetail person;
        private Boolean isLoading = false;

        public PersonDetailViewModel(IMyStudyService service)
        {
            this.service = service;
            this.person = new PersonDetail();
        }

        public PersonDetailViewModel(IMyStudyService service, PersonDetail person)
        {
            this.service = service;
            this.person = person;
        }

        public int Id
        {
            get { return person.Id; }
            set
            {
                person.Id = value;
                NotifyPropertyChanged("Id");
            }
        }

        public String FirstName
        {
            get { return person.FirstName; }
            set
            {
                person.FirstName = value;
                NotifyPropertyChanged("FirstName");
            }
        }

        public String LastName
        {
            get { return person.LastName; }
            set
            {
                person.LastName = value;
                NotifyPropertyChanged("LastName");
            }
        }

        public String FullName
        {
            get { return FirstName + " " + LastName; }
        }

        public String EmailAddress
        {
            get { return person.EmailAddress; }
            set
            {
                person.EmailAddress = value;
                NotifyPropertyChanged("EmailAddress");
            }
        }

        public String Office
        {
            get { return person.Office; }
            set
            {
                person.Office = value;
                NotifyPropertyChanged("Office");
            }
        }

        public String OfficeHours
        {
            get { return person.OfficeHours; }
            set
            {
                person.OfficeHours = value;
                NotifyPropertyChanged("OfficeHours");
            }
        }

        public String Phone
        {
            get { return person.Phone; }
            set
            {
                person.Phone = value;
                NotifyPropertyChanged("Phone");
            }
        }

        public String Mobile
        {
            get { return person.Mobile; }
            set
            {
                person.Mobile = value;
                NotifyPropertyChanged("Mobile");
            }
        }

        public String Website
        {
            get { return person.Website; }
            set
            {
                person.Website = value;
                NotifyPropertyChanged("Website");
            }
        }

        public String ShortCv
        {
            get { return person.ShortCv; }
            set
            {
                person.ShortCv = value;
                NotifyPropertyChanged("ShortCv");
            }
        }

        public String EducationInterests
        {
            get { return person.EducationInterests; }
            set
            {
                person.EducationInterests = value;
                NotifyPropertyChanged("EducationInterests");
            }
        }

        public String ResearchInterests
        {
            get { return person.ResearchInterests; }
            set
            {
                person.ResearchInterests = value;
                NotifyPropertyChanged("ResearchInterests");
            }
        }

        public String ImageUrl
        {
            get { return person.ImageUrl; }
            set
            {
                person.ImageUrl = value;
                NotifyPropertyChanged("ImageUrl");
            }
        }

        public Uri ImageUri
        {
            get {
                if(String.IsNullOrEmpty(ImageUrl)) {
                    return null;
                }
                return new Uri(ImageUrl);
            }
        }

        public Boolean IsLoading
        {
            get { return isLoading; }
            set
            {
                isLoading = value;
                NotifyPropertyChanged("IsLoading");
            }
        }

        public void Refresh()
        {
            NotifyPropertyChanged(string.Empty);
        }

        public void LoadPerson(int id)
        {
            App.MyStudyService.GetPerson(id, (result, person) =>
            {
                this.person = person;

                Refresh();
            });
        }
    }
}
